require 'test/unit'

require 'models/region'
require 'models/account'
require 'models/container'
require 'database/game_database'
require 'engine/game_setup'

class TCGame
    include GameDatabaseSetup
end
class TCChar < Entity
    include BelongsToAccount
    uses_templates_from CharacterDatabase
    def player?
        self.account!=nil
    end
end
class TCTemplate < Entity
    def create_instance
        inst = TCChar.new
        inst.name = 'TChar'
        inst.template = self
        inst
    end
end

# to make test indempotent
class VectorDatabase
    def reset; @container = [] end
end
class MapDatabase
    def reset; @container = {} end
end
class TemplateInstanceDatabase
    def reset
        @instances.reset
        @templates.reset
    end
end

class GameDatabaseSetupTest < Test::Unit::TestCase
    require 'fileutils'
    def setup
        @root_dir = ['test','game'].join(File::SEPARATOR)
        @game = TCGame.new
        region_dir = [@root_dir,'regions'].join(File::SEPARATOR)
        template_dir = [@root_dir,'templates'].join(File::SEPARATOR)
        @players_dir = [@root_dir,'players'].join(File::SEPARATOR)
        FileUtils.mkdir_p(region_dir) unless File.exist?(region_dir)
        FileUtils.mkdir_p(template_dir) unless File.exist?(template_dir)
        FileUtils.mkdir_p(@players_dir) unless File.exist?(@players_dir)
    end
    def teardown
        #RegionDatabase.instance.reset
        #CharacterDatabase.instance.reset
        #AccountDatabase.instance.reset
    end

=begin
    def test_region_can_be_saved
        r = Region.new
        r.name = "A Region"

        assert(RegionDatabase.instance.add(r))

        rr = RegionDatabase.instance.to_a[0]
        assert_equal(r,rr)

        assert(@game.save_the_game(nil,@root_dir))
    end
=end
    def test_region_can_be_loaded
        @game.load_the_game(@root_dir)
        assert_equal(2,RegionDatabase.instance.size)

        #r = RegionDatabase.instance.to_a[0]
        #assert_equal("A Region", r.name)
        #assert_equal(0, r.oid)
    end
=begin
    def test_region_can_add_more
        @game.load_the_game(@root_dir)

        r2 = Region.new
        r2.oid = 2
        r2.name = "Another Region"

        assert(RegionDatabase.instance.add(r2))
        assert(2,RegionDatabase.instance.size)

        assert(@game.save_the_game(nil,@root_dir))
        
        # cleanup
        FileUtils.remove_dir(@root_dir)
    end

    def test_templates_can_be_saved
        tmpl = TCTemplate.new
        tmpl.oid = 1
        tmpl.name = 'A Template'
        CharacterDatabase.instance.use_templates([tmpl])
        assert(@game.save_the_game(nil,@root_dir))
    end

    def test_templates_can_be_loaded
        test_templates_can_be_saved
        assert(CharacterDatabase.instance.to_a.empty?)
        assert(CharacterDatabase.instance.templates.to_a.empty?)

        @game.load_the_game(@root_dir)
        
        assert_equal(1,CharacterDatabase.instance.templates.size)

        tmpl = CharacterDatabase.instance.find_template_by_name('A Template')
        char = CharacterDatabase.instance.generate_from_template(tmpl)

        assert_equal(1,CharacterDatabase.instance.to_a.size)
        assert_equal('A Template',CharacterDatabase.instance.to_a.first.template.name)

    end

    def test_character_vs_player
        tmpl = TCTemplate.new
        tmpl.name = 'A Template'
        CharacterDatabase.instance.use_templates([tmpl])
        assert(@game.save_the_game(nil,@root_dir))
        assert_equal(1,CharacterDatabase.instance.templates.size)

        tmpl = CharacterDatabase.instance.find_template_by_name('A Template')
        char = CharacterDatabase.instance.generate_from_template(tmpl)

        assert_equal(1,CharacterDatabase.instance.to_a.size)
        assert_equal('A Template',CharacterDatabase.instance.to_a.first.template.name)

        acct = Account.new; acct.oid = 1; acct.name = 'Account'
        AccountDatabase.instance.add(acct)
        char2 = CharacterDatabase.instance.generate_from_template(tmpl)
        char2.account = acct

        assert(@game.save_the_game(nil,@root_dir))
        assert_equal(2,CharacterDatabase.instance.to_a.size)
        assert(File.exist?("#{@players_dir+File::SEPARATOR+char2.name}.yaml"))

    end
=end
end
